/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author nguyenquang
 */
public class loginServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String username = request.getParameter("txtUsername");
            String password = request.getParameter("txtPassword");
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            if (user.login()) {
                Cookie ckUsername = new Cookie("username", username);
                Cookie ckPassword = new Cookie("password", password);
                ckUsername.setMaxAge(999999999);
                ckPassword.setMaxAge(999999999);
                response.addCookie(ckPassword);
                response.addCookie(ckUsername);

                Cookie[] listCookie = request.getCookies();
                int count = 1;
                boolean flag = false;                

                for(int i=0;i<listCookie.length;i++)
                {                
                    if(listCookie[i].getName().equals("count"))
                    {                      
                        count = Integer.parseInt(listCookie[i].getValue());
                        count++;
                        listCookie[i].setValue(new Integer(count).toString());
                        response.addCookie(new Cookie("count", new Integer(count).toString()));
                        flag = true;
                        break;
                    }
                }

                if(flag == false)
                {
                    Cookie cookie = new Cookie("count", new String("1"));
                    response.addCookie(cookie);
                    out.println("<h1>Welcome, " + user.getUsername() + " !</h1>");
                }

                if (count == 5)
                    out.println("<h1>soon!! You will receive a gift item</h1>");
                out.println("<br>You have login " + String.valueOf(count) + " times !");
                out.print("<br><a href='index.jsp'>index</a>");
            } else {
                response.sendRedirect("errorLogin.jsp");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
